<?php

class Admin_HistoryController extends Zend_Controller_Action
{
	private $_auth;
	private $_acl;
    public function init(){
        $acl                = new My_Acl();
        $auth               = $acl->checkAllowed($this->_request);
        $this->_auth        = $auth->getIdentity();
        $this->view->auth   = $this->_auth;
        $this->_acl         = $acl;
        $this->view->acl    = $this->_acl;
    }
    public function indexAction(){
        if(!file_exists(CONFIG_PATH . '/history.ini')){
            exit;
        }
        $configHistory = new Zend_Config_Ini(CONFIG_PATH . '/history.ini', APPLICATION_ENV);
        $this->view->configHistory = $configHistory;
        
        $modelGame = new Admin_Model_Game();
        $gameList = $modelGame->getAllData()->toArray();
        $this->view->gameList = $gameList;
        $modelUser = new Admin_Model_User();
        $userList = $modelUser->getAllData()->toArray();
        $this->view->userList = $userList;
        $option['userId'] = '';
        $option['mod'] = '';
        //$action = '';
        $option['startTime'] = '';
        $option['endTime'] = '';
        if($this->getRequest()->isGet()){
            $formData = $this->getRequest()->getParams();
            if($formData){
                if(isset($formData['userId']) && is_numeric($formData['userId']) && $formData['userId'] > 0){
                    $option['userId'] = $formData['userId'];
                    $this->view->userId = $option['userId'];
                }
                if(isset($formData['mod'])){
                    $option['mod'] = $formData['mod'];
                    $this->view->mod = $option['mod'];
                }
                $gameIdList = array(); 
                foreach($gameList as $game){
                    $gameIdList[] = $game['gameId'];
                }
                if(isset($formData['gameS']) && !is_array($formData['gameS'])){
                    $option['gameS'][] = $formData['gameS'];
                }elseif(isset($formData['gameS']) && is_array($formData['gameS']) && $formData['gameS']){
                    $option['gameS'] = $formData['gameS'];
                }
                if(isset($option['gameS']) && is_array($option['gameS'])){
                    foreach($option['gameS'] as $key=>$row){
                        if(!in_array($row,$gameIdList)){
                            unset($option['gameS'][$key]);
                        }
                    }
                    $this->view->gameS = $option['gameS'];
                }
                //if(isset($formData['action']))
                    //$action = $formData['action'];
                if(isset($formData['startTime']) && $formData['startTime']){
                    $option['startTime']= strtotime($formData['startTime']);
                    $this->view->startTime = $formData['startTime'];
                }
                if(isset($formData['endTime']) && $formData['endTime']){
                    $option['endTime'] = strtotime($formData['endTime']);
                    $this->view->endTime = $formData['endTime'];
                }
            }
        }
        $modelHistory = new Admin_Model_History();
        $total = $modelHistory->countSearchData($option);
        
        $page                           = $this->_getParam('page', 1);
        $itemPerPage                    = 20;
        $pageRange                      = 10;
        $offset                         = 0;
        if($page > 0){
            if($total > 0){
                $paginator              = new My_Paginator();
                if(isset($config['pagination']['itemPerPage']))
                    $itemPerPage        = $config['pagination']['itemPerPage'];
                if(isset($config['pagination']['pageRange']))
                    $pageRange          = $config['pagination']['pageRange'];

                $offset                 = ($page-1)*$itemPerPage;
                $createPaginator        = $paginator->createPaginator($total, $itemPerPage, $pageRange, $page);
                $this->view->paginator  = $createPaginator;
                //prTien($createPaginator);
            }
        }
        $option['limit']                = "$itemPerPage,$offset";
        $historyList = $modelHistory->getSearchData($option);
        $this->view->historyList = $historyList;
    }
}